package com.it2297.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import com.it2297.component.AbstractPagination;
import com.it2297.controller.JobController;
import com.it2297.controller.ProductController;
import com.it2297.controller.UsedItemController;
import com.it2297.main.UserApp;
import com.it2297.model.Job;
import com.it2297.model.Product;
import com.it2297.renderer.ApplicationCellRenderer;
import com.it2297.renderer.ProductCellRenderer;
import javax.swing.JLabel;
import java.awt.Component;
import javax.swing.Box;
import java.awt.Font;
import java.awt.Color;
import javax.swing.UIManager;

public class ViewShortlistedApplications extends JPanel {

	final JobController jct= new JobController(this);

	private final JTable table = new JTable();
	private final DefaultTableModel tableModel = new DefaultTableModel(0, 2) {
		@Override
		public boolean isCellEditable(int row, int column) {
			return false;
		}
	};
	private final ApplicationCellRenderer tableCellRenderer = new ApplicationCellRenderer();
	private final AbstractPagination pagination = new ViewPagination();
	private final JToolBar toolBar = new JToolBar();
	private final Job job = new Job();
	private final JButton btnRefresh = new JButton("Refresh");
	private final JLabel lbl = new JLabel();
	

	public ViewShortlistedApplications() {
		initGUI();
	}

	
	
	private void initGUI() {
		this.setBounds(100, 100, 740, 560);
		setLayout(null);
		table.setBounds(0, 30, 740, 530);
		this.add(table);
		this.add(pagination, BorderLayout.SOUTH);
		String currentWorkingDirectory = System.getProperty("user.dir");

		table.setRowHeight(100);
		table.setShowGrid(false);
		table.setFocusable(false);
		table.setModel(tableModel);

		table.getColumnModel().getColumn(0).setMaxWidth(450);
		table.getColumnModel().getColumn(1).setMaxWidth(300);
		table.getColumnModel().getColumn(0).setCellRenderer(tableCellRenderer);
		table.getColumnModel().getColumn(1).setCellRenderer(tableCellRenderer);
		
		
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent mouseEvent) {
				if (mouseEvent.getClickCount() > 1) {
				//	comView.refresh();
				//	controller.showProductDetail();
				//	JOptionPane.showMessageDialog(null, "Testing");
				}
			}
		});
		toolBar.setBounds(0, 0, 740, 27);
		toolBar.setBackground(UIManager.getColor("Button.background"));
		toolBar.setFloatable(false);

		add(toolBar);
		lbl.setForeground(new Color(255, 255, 255));
		lbl.setFont(new Font("Times New Roman", Font.BOLD, 15));
		
		
		
		//lbl.setText("Currently viewing all applications made by: " + UserApp.getCurrentUser().getName() + " (" + UserApp.getCurrentUser().getNric() + ").");
		toolBar.add(lbl);
		btnRefresh.setBackground(Color.LIGHT_GRAY);
		btnRefresh.setFont(new Font("Times New Roman", Font.BOLD, 15));
		
		//btnRefresh.setIcon(new ImageIcon(currentWorkingDirectory + "/img/Job/refresh.gif"));
		btnRefresh.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				pagination.setTotal(jct.getTotalNumOfShortlistedByNeedy());
				pagination.refresh();
				if(jct.getTotalNumOfShortlistedByNeedy()==0){
						JOptionPane.showMessageDialog(null, "You do not have anymore pending applications!");
						table.setVisible(false);
					}
				else if(jct.getTotalNumOfShortlistedByNeedy()!=0){
					table.setVisible(true);
				}
			}
		});
		
		JLabel label = new JLabel("");
		label.setFont(new Font("Arial", Font.BOLD, 13));
		label.setText("You have been shortlisted for interview by " + jct.getTotalNumOfShortlistedByNeedy() + " job(s).");
		toolBar.add(label);
		
		Component horizontalStrut = Box.createHorizontalStrut(325);
		toolBar.add(horizontalStrut);
		
		toolBar.add(btnRefresh);
		
	
	
	}

	public Object getSelectedItem() {
		return table.getValueAt(table.getSelectedRow(),
				table.getSelectedColumn());
	}

	public Object[] getSelectedItems() {
		ArrayList<Object> items = new ArrayList<Object>();
		for (int i : table.getSelectedRows()) {
			items.add(table.getValueAt(i, table.getSelectedColumn()));
		}
		return items.toArray();
	}

	
	
	private class ViewPagination extends AbstractPagination {
		protected ViewPagination() {
			this.setCount(5);
			//this.setTotal(controller.getRecordNumProduct());
			this.setTotal(jct.getTotalNumOfShortlistedByNeedy());
		}

		@Override
		protected void action(Object[] data) {
			// clear all row
			tableModel.setRowCount(0);

			// add new row base on data size
			for (int i = 0; i < data.length; i++) {
				tableModel.addRow(new Object[] { data[i], data[i] });
			}
		}

		@Override
		protected Object[] subData(int position, int count) {
			return jct.getMultipleShortlisted(position, count).toArray();
		}
	}

	{

	}

	public void addProductListener(MouseListener productcellRenderer) {
		table.addMouseListener(productcellRenderer);
	}

	public Product getSelected() {
		return (Product) table.getModel().getValueAt(table.getSelectedRow(),
				table.getSelectedColumn());
	}
	
	public  void refresh(){
		pagination.refresh();
	}
	
	public String getNric(){
		return UserApp.getCurrentUser().getNric();
	}
}
